feat(consensus): tipset gas reservations and reservation‑aware mpool pre‑pack #13427
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
feat(consensus): tipset gas reservations and reservation‑aware mpool pre‑pack
This PR wires Lotus into the engine‑managed tipset gas reservation design described in a pending FIP proposal, and adds an optional reservation‑aware mempool pre‑pack pass. The goal is to eliminate miner‑charged underfunded messages in a tipset while preserving receipts and gas outputs.
Summary
Σ(gas_limit * gas_fee_cap)), deduped by CID across blocks.This branch assumes matching
multistage-executionbranches inref-fvm(engine reservations) andfilecoin-ffi(Begin/End FFI) are present.Changes
Consensus and activation
chain/consensus/features.goReservationFeatureFlagswith:MultiStageReservations(best‑effort pre‑activation enablement).MultiStageReservationsStrict(pre‑activation behaviour: legacy fallback vs tipset‑invalidating).Featureglobals with env‑based defaults:LOTUS_ENABLE_TIPSET_RESERVATIONSLOTUS_ENABLE_TIPSET_RESERVATIONS_STRICTchain/consensus/reservations.goReservationsEnabled(nv):nv >= ReservationsActivationNetworkVersion()→ always true (reservations required).Feature.MultiStageReservations.buildReservationPlan(bms []FilecoinBlockMessages) map[address.Address]abi.TokenAmount:gas_limit * gas_fee_capper sender.startReservations/endReservations:vmi.StartTipsetReservations/EndTipsetReservationswhen enabled.metrics.ReservationPlanSendersandmetrics.ReservationPlanTotal.handleReservationErrorto decide, pre‑activation:ErrReservationsNotImplemented.ErrReservationsInsufficientFunds/ErrReservationsPlanTooLarge:ErrReservationsNotImplementedbecomes a node error.chain/consensus/compute_state.gostartReservations(ctx, vmi, bms, nv)(before applying any explicit messages).deferedendReservations(ctx, vmi, nv)plus an explicitendReservationscall before cron to scope reservations to explicit messages only.VM interface and FVM wiring
chain/vm/vmi.go&chain/vm/execution.govm.Interfacewith:StartTipsetReservations(ctx context.Context, plan map[address.Address]abi.TokenAmount) errorEndTipsetReservations(ctx context.Context) errorvmExecutor.StartTipsetReservations/EndTipsetReservationsby:ApplyMessage/ApplyImplicitMessage).chain/vm/fvm.gofilecoin-ffiand define:ReservationsActivationNetworkVersion()(currentlynetwork.Version28).func (vm *FVM) BeginReservations(plan []byte) errorfunc (vm *FVM) EndReservations() errorfunc (vm *FVM) StartTipsetReservations(ctx context.Context, plan map[address.Address]abi.TokenAmount) errorfunc (vm *FVM) EndTipsetReservations(ctx context.Context) error[[address_bytes, amount_bytes], ...].ffi.FVM.BeginReservations/ffi.FVM.EndReservations, which return typed errors potentially wrapped with short engine‑provided messages from ref‑fvm.reservationsActiveto avoid calling End when no Begin occurred (empty plan or legacy fallback).ErrReservationsNotImplementedas a benign signal and fall back to legacy mode.handleReservationError.ErrReservationsNotImplementedas a node error (engine too old).chain/vm/vm.goStartTipsetReservations/EndTipsetReservationsonLegacyVMto satisfy the interface; reservations are only supported in FVM mode.chain/vm/fvm_reservations_test.goreservationStatusToErrormapping from raw status codes to typed errors.Mempool reservation‑aware pre‑pack (optional)
chain/types/mpool.go&chain/messagepool/config.goEnableReservationPrePacktoMpoolConfig, defaultfalse.chain/messagepool/selection.goselectedMessageswith:reservationEnabled,reservationCtx,reservationTipset.reservedBySender(Σ(cap×limit)per sender for selected messages).balanceBySender(sender balances at the selection base tipset).reservationsPrePackEnabled() bool:EnableReservationPrePackandconsensus.ReservationsEnabled(nextNv).initReservations(ctx, ts, mp)to initialize tracking maps when enabled.reserveForMessages(sender, msgs)to:mp.api.StateGetActor/GetActorAfterand cache it.reserveForMessagesfromtryToAdd/tryToAddWithDepsto:chain/messagepool/selection_test.goTestReservationPrePackPrunesOverCommittedChain:TestReservationPrePackAccumulatesCapTimesLimit:Metrics and docs
metrics/metrics.goReservationPlanSenders(vm/reservations_plan_senders) – number of unique senders in the plan.ReservationPlanTotal(vm/reservations_plan_total_atto) – total reserved Σ(cap×limit) in attoFIL.documentation/en/tipset-reservations.mdnetwork.Version28/UpgradeXxHeight.CHANGELOG.mdActivation & gating
Feature.MultiStageReservations= false → legacy mode (no Begin/End, no pre‑pack).Feature.MultiStageReservations= true,MultiStageReservationsStrict= false:Feature.MultiStageReservations= true,MultiStageReservationsStrict= true:ErrReservationsNotImplemented, which still triggers legacy fallback.ReservationsEnabledalways true.ErrReservationsNotImplementedis treated as a node error (engine too old).Testing
chain/consensus/reservations_test.go– plan build, env/feature gating, error handling paths.chain/vm/fvm_reservations_test.go– status‑to‑error mapping, CBOR plan encode/decode.chain/messagepool/selection_test.go– reservation pre‑pack heuristics.go testruns (already exercised locally on this branch):go test ./chain/consensus -run TestReservationgo test ./chain/vm -run TestReservationgo test ./chain/messagepool -run TestReservationNotes
ref-fvmmultistage-execution(engine reservation session + enforcement).filecoin-ffimultistage-execution(Begin/End FFI + error messages).ExitCode,GasUsed, events) andGasOutputsremain unchanged; the reservation ledger is internal to the engine.